<?php
/**
 * 注：此模板为自动生成模板，需要根据实际情况调整修改
 * 此模板对 无主键/单一主键 表的支持较好，联合主键表在下一版本开发
 */
namespace Data;

use Utils\MyException;

class Order_for_foods_DAO {

    public static $TABLENAME = 'order_for_foods';

    public static $_db;

    public static function query($str)
    {
        if(self::$_db === null) {
            self::$_db = \GatewayWorker\Lib\Db::instance('main');
            return self::$_db->query($str);
        } else {
            return self::$_db->query($str);
        }
    }



    public static function getRecordByFormIdFoodId($order_form_id,$food_id){
        $where = "`ORDER_FORM_ID` = '$order_form_id' AND `FOOD_ID`='$food_id'";
        $record = self::query('select * from '.static::$TABLENAME .' where '.$where);
        if($record != null){
            return new Order_for_foods($record[0]);
        }
        return null;
    }


    public static function getList() {
        $where = "";
        $list = self::query('select * from '.static::$TABLENAME);
        $results = array();

        foreach($list as $record){
            $results[] = new Order_for_foods($record);
        }
        return $results;
    }


    public static function getRetreatList($order_form_id) {
        $where = "`ORDER_FORM_ID` = '$order_form_id' AND `SUC_RETREAT_COUNT`>0";
        $list = self::query('select * from '.static::$TABLENAME.' where '.$where);
        return $list;
    }


    public static function getListByOrderFormId($order_form_id) {
        $where = "`ORDER_FORM_ID` = '$order_form_id'";
        $list = self::query('select * from '.static::$TABLENAME .' where '.$where);
        return $list;
    }

    public static function getListByOrderFormIdRecord($order_form_id) {
        $where = "`ORDER_FORM_ID` = '$order_form_id'";
        $list = self::query('select * from '.static::$TABLENAME .' where '.$where);
        $results = array();

        foreach($list as $record){
            $results[] = new Order_for_foods($record);
        }
        return $results;
    }

    public static function getRetreattingLis($order_form_id) {
        $where = "`ORDER_FORM_ID` = '$order_form_id' AND `RETREAT_STATUS`=".RETREAT_FOOD_STATUS_START;
        $list = self::query('select * from '.static::$TABLENAME .' where '.$where);
        $results = array();
        foreach($list as $record){
            $results[] = new Order_for_foods($record);
        }
        return $results;
    }


    public static function getFoodsList($restaurant_id,$yesterday_date) {
        $where = "`RESTAURANT_ID`='$restaurant_id' AND `DATE`='$yesterday_date' AND `PAY_STATUS`=".STATE_YES." group by `FOOD_ID`";
        $list1 = self::query('select `FOOD_ID`,SUM(FOOD_COUNT) AS '.FOOD_COUNT.',SUM(FOOD_COUNT*FOOD_PRICE) AS '.FOOD_PRICE.' from '.static::$TABLENAME.' where '.$where.' order by '.FOOD_COUNT.' desc');
        $list2 = self::query('select `FOOD_ID`,SUM(FOOD_COUNT) AS '.FOOD_COUNT.',SUM(FOOD_COUNT*FOOD_PRICE) AS '.FOOD_PRICE.' from '.static::$TABLENAME.' where '.$where.' order by '.FOOD_PRICE.' desc');
        $results = array();
        $results['order_by_count'] = $list1;
        $results['order_by_price'] = $list2;
        return $results;
    }



    public static function insert($record){
        try {
            $sql = 'insert into '.static::$TABLENAME .' values ('.$record->toInsertCSV().')';
            self::query($sql);
        } catch(MyException $e){
            throw $e;
        }
    }

    public static function update($record){
        try{
            $ID = $record->getORDER_FORM_ID();
            $FOOD_ID = $record->getFOOD_ID();
            $where = "`ORDER_FORM_ID` = '$ID' AND `FOOD_ID`='$FOOD_ID'";
            $oldrecord = self::getRecordByFormIdFoodId($ID,$FOOD_ID);
            if($oldrecord == null){
                throw new MyException("不存在该记录");
            }
            $sql = 'update '.static::$TABLENAME .' set '.$record->toUpdateSet().' where '.$where ;
            self::query($sql);
        } catch(MyException $e){
            throw $e;
        }
    }

}

